

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:furaha_mkopo/commonTool/fm_language_tool.dart';
import 'package:furaha_mkopo/page/base/fm_base_page_widget.dart';
import 'package:furaha_mkopo/page/dialog/address/fm_new_address_page.dart';
import 'package:geolocator_platform_interface/src/models/position.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';

import '../../../../commonTool/fm_color_tool.dart';
import '../../../../commonTool/fm_screen_tool.dart';
import '../../../../commonUI/fm_bottom_button.dart';
import '../../../../commonUI/fm_custon_nav_bar_widget.dart';
import '../../../../commonUI/fm_detail_base_cell_widget.dart';
import '../../../../commonUI/fm_detail_step_widget.dart';
import '../../../../commonUI/fm_progress_hud.dart';
import '../../../dialog/address/fm_select_address_page.dart';
import '../../../dialog/single/fm_single_radio_page.dart';
import '../personal/fm_personal_model.dart';
import 'fm_job_controller.dart';

class FMJobPage extends FMBasePageWidget {

  String? title;

  String orderId;

  bool? isOldPage;

  FMJobPage({super.key,required this.orderId,this.title,this.isOldPage});

  @override
  State<FMJobPage> createState() => _FMJobPageState();
}

class _FMJobPageState extends FMBasePageState<FMJobPage> {

  final controller = Get.put(FMJobController());

  @override
  void initState() {
    super.initState();
    controller.refreshData(widget.orderId);

    FMProgressHud.showLoading();
  }

  @override
  void dispose() {
    Get.delete<FMJobController>();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: FMColorTool.color("#54E041"),
      resizeToAvoidBottomInset: false,
      body: GetBuilder<FMJobController>(builder: (controller){
        return _bodyWidget();
      }),
    );
  }

  Widget _bodyWidget() {
    return GestureDetector(
      onTap: (){
        FocusScope.of(context).unfocus();
      },
      child: Stack(
        children: [
          _topWidget(),
          _contentWidget(),
          _bottomWidget()
        ],
      ),
    );
  }

  Widget _topWidget() {
    return Positioned(
      top: FMScreenTool.topPadding(context),
      left: 0,
      right: 0,
      child: FMCustomNavBarWidget(
          title: widget.title ?? ""
      ),
    );
  }

  Widget _contentWidget() {
    return Positioned(
      top: FMScreenTool.appBarHeight(context) + 14,
      bottom: FMScreenTool.bottomPadding(context) + 173,
      left: 16,
      right: 16,
      child: Container(
        decoration: const BoxDecoration(
            image: DecorationImage(
                image: AssetImage("assets/detail/personal_bg.png"),
                fit: BoxFit.cover
            )
        ),
        child: Column(
          children: [
            Padding(
              padding: const EdgeInsets.only(top: 12,left: 11,right: 11),
              child: FMDetailStepWidget(
                  title: FMLanguageTool.getText("Step 3")
              ),
            ),
            const SizedBox(height: 16),
            _listWidget()
          ],
        ),
      ),
    );
  }

  Widget _listWidget() {
    if (controller.dataSource != null && controller.dataSource!.isNotEmpty) {
      return Expanded(
        child: SingleChildScrollView(
          padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
          child: Column(
            children: controller.dataSource!.map((e){
              return Padding(
                padding: const EdgeInsets.only(left: 12,right: 12,bottom: 12),
                child: GetBuilder<FMJobController>(
                    id: "updateCell",
                    builder: (controller){
                      return FMDetailBaseCellWidget(
                        title:e.soul ?? "",
                        content:controller.contentAtIndex(e),
                        placeHolder:e.perilous ?? "",
                        isClicked: controller.isNeedClicked(e),
                        inputType: controller.textInputType(e),
                        onTap: (){
                          popShow(e);
                        },
                        onChanged: (text){
                          controller.updateDic(e, text);
                        },
                      );
                    }),
              );
            }).toList(),
          ),
        ),
      );
    }else {
      return Container();
    }
  }

  Widget _bottomWidget() {
    return Positioned(
      left: 38,
      right: 38,
      bottom: FMScreenTool.bottomPadding(context),
      child: FMBottomButton(
        title: FMLanguageTool.getText("Next"),
        onTap: (){
          controller.saveInfo(context,widget.isOldPage);
        },
      ),
    );
  }

  void popShow(FMPersonalIncidentsModel? model) {
    FocusScope.of(context).unfocus();
    if (model?.sent == "could") {
      singlePopShow(model,model?.retired);
    }else if (model?.sent == "wife") {
      cityPopShow(model);
    }
  }

  void singlePopShow(FMPersonalIncidentsModel? model,List<FMPersonalRetiredModel>? retired) {
    if (retired == null || retired.isEmpty) return;

    List<String> list = [];

    for (FMPersonalRetiredModel model in retired) {
      if (model.planned != null && model.planned!.isNotEmpty) {
        list.add(model.planned!);
      }
    }

    Get.to(() => FMSingleRadioPage(
      selectData: controller.contentAtIndex(model),
      mList: list,
      onTap: (text){
        controller.updateDic(model,text);
      },
    ),
        opaque: false, duration: Duration.zero);
  }

  void cityPopShow(FMPersonalIncidentsModel? model) {
    FMNewAddressPage.showAddress("", (addressValue){
      controller.updateDic(model, addressValue);
    });
  }

  @override
  void onLocationError(String error) {
  }

  @override
  void onLocationUpdated(Position position) {
    controller.position = position;
  }
}
